package exception;

public class IllegalNameException extends Exception {

	static final private String MESSAGE_FORMAT_DUPLICATE_TABLE = "Table with name %s already exists in database %s.";
	static final private String MESSAGE_FORMAT_DUPLICATE_FIELD = "Field with name %s already exists in table %s.";
	static final private String MESSAGE_FORMAT_DUPLICATE_DATABASE = "Database with name %s already exists.";
	static final private String MESSAGE_NAME_NULL = "Name cannot be NULL";
	
	private String message;

	@Override
	public String getMessage() {
		return message;
	};

	private static final long serialVersionUID = -6811886080432105065L;

	public static IllegalNameException databaseExists(String databaseName) {
		IllegalNameException ex = new IllegalNameException();
		ex.message = String.format(MESSAGE_FORMAT_DUPLICATE_DATABASE, databaseName);
		return ex;
	}
	
	public static IllegalNameException tableExists(String databaseName, String tableName) {
		IllegalNameException ex = new IllegalNameException();
		ex.message = String.format(MESSAGE_FORMAT_DUPLICATE_TABLE, tableName, databaseName);
		return ex;
	}
	
	public static IllegalNameException fieldExists(String tableName, String fieldName) {
		IllegalNameException ex = new IllegalNameException();
		ex.message = String.format(MESSAGE_FORMAT_DUPLICATE_FIELD, fieldName, tableName);
		return ex;
	}

	public static IllegalNameException nameCannotBeNull() {
		IllegalNameException ex = new IllegalNameException();
		ex.message = MESSAGE_NAME_NULL;
		return ex;
	}

	public static IllegalNameException fromDBValueException(DBValueException e) {
		IllegalNameException ex = new IllegalNameException();		
		ex.message = e.getMessage();
		return ex;
	}
	
	
}
